home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 2.iso / dist / fw_glimpse.idb / usr / freeware / src / glimpse-3.0 / agrep / asearch1.c.z / asearch1.c
C/C++ Source or Header  |  1997-09-09  |  14KB  |  432 lines

  1. /* Copyright (c) 1994 Sun Wu, Udi Manber, Burra Gopal.  All Rights Reserved. */
  2. #include "agrep.h"
  3.  
  4. extern unsigned Init1, Init[], Mask[], endposition, D_endpos;
  5. extern unsigned NO_ERR_MASK;
  6. extern int TRUNCATE, DELIMITER, AND, I, S, DD, INVERSE, FILENAMEONLY ;
  7. extern char CurrentFileName[];
  8. extern int num_of_matched, prev_num_of_matched;
  9.  
  10. extern int CurrentByteOffset;
  11. extern CHAR *agrep_inbuffer;
  12. extern int agrep_inlen;
  13.  
  14. extern FILE *agrep_finalfp;
  15. extern CHAR *agrep_outbuffer;
  16. extern int agrep_outlen;
  17. extern int agrep_outpointer;
  18.  
  19. extern int NEW_FILE, POST_FILTER;
  20.  
  21. extern int LIMITOUTPUT, LIMITPERFILE;
  22.  
  23. int
  24. asearch1(old_D_pat, Text, D)
  25. char old_D_pat[]; 
  26. int Text; 
  27. register unsigned D;
  28. {
  29.     register unsigned end, i, r1, r3, r4, r5, CMask, D_Mask, k, endpos; 
  30.     register unsigned r_NO_ERR;
  31.     unsigned A[MaxError*2+1], B[MaxError*2+1];
  32.     int D_length, ResidueSize, lasti, num_read,  FIRSTROUND=1, j=0;
  33.     CHAR *buffer;
  34.     /* CHAR *tempbuf = NULL;*/    /* used only when Text == -1 */
  35.  
  36.     if(I == 0) Init1 = (unsigned)037777777777;
  37.     if(DD > D) DD = D+1;
  38.     if(I  > D) I  = D+1;
  39.     if(S  > D) S  = D+1;
  40.     D_length = strlen(old_D_pat);
  41.  
  42.     r_NO_ERR = NO_ERR_MASK;
  43.  
  44.     D_Mask = D_endpos;
  45.     for(i=1; i<D_length; i++) D_Mask = (D_Mask << 1) | D_Mask;
  46.     D_Mask = ~D_Mask;
  47.     endpos = D_endpos;
  48.     r3 = D+1; 
  49.     r4 = D*2;  /* to make sure in register */
  50.     for(k=0; k < D;   k++) A[k] = B[k] = 0;
  51.     for(k=D; k <= r4; k++) A[k] = B[k] = Init[0];
  52.  
  53. #if    AGREP_POINTER
  54.     if (Text != -1) {
  55. #endif    /*AGREP_POINTER*/
  56.         lasti = Max_record;
  57.         alloc_buf(Text, &buffer, BlockSize+Max_record+1);
  58.         buffer[Max_record-1] = '\n';
  59.  
  60.         while ((num_read = fill_buf(Text, buffer + Max_record, BlockSize)) > 0)
  61.         {
  62.             i=Max_record; 
  63.             end = Max_record + num_read;
  64.             if(FIRSTROUND) { 
  65.                 i = Max_record -1 ;
  66.                 if(DELIMITER) {
  67.                     for(k=0; k<D_length; k++) {
  68.                         if(old_D_pat[k] != buffer[Max_record+k]) break;
  69.                     }
  70.                     if(k>=D_length) j--;
  71.                 }
  72.                 FIRSTROUND = 0; 
  73.             }
  74.             if(num_read < BlockSize) {
  75.                 strncpy(buffer+Max_record+num_read, old_D_pat, D_length);
  76.                 end = end + D_length;
  77.                 buffer[end] = '\0';
  78.             }
  79.  
  80.             /* ASEARCH1_PROCESS: the while-loop below */
  81.             while (i < end)
  82.             {
  83.                 CMask = Mask[buffer[i++]];
  84.                 CurrentByteOffset ++;
  85.                 r1 = Init1 & B[D];
  86.                 A[D] = ((B[D] >> 1) & CMask )  | r1;
  87.                 for(k = r3; k <= r4; k++)  /* r3 = D+1, r4 = 2*D */
  88.                 { 
  89.                     r5 = B[k];
  90.                     r1 = Init1 & r5;
  91.                     A[k] = ((r5 >> 1) & CMask) | B[k-I] | (((A[k-DD] | B[k-S]) >>1) & r_NO_ERR) | r1 ; 
  92.                 }
  93.                 if(A[D] & endpos) {  
  94.                     j++;
  95.                     if (DELIMITER) CurrentByteOffset -= D_length;
  96.                     else CurrentByteOffset -= 1;
  97.                     if(((AND == 1) && ((A[D*2] & endposition) == endposition)) || ((AND == 0) && (A[D*2] & endposition)) ^ INVERSE )
  98.                     { 
  99.                         if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
  100.                             num_of_matched++;
  101.                             if (agrep_finalfp != NULL) 
  102.                                 fprintf(agrep_finalfp, "%s\n", CurrentFileName);
  103.                             else {
  104.                                 int outindex;
  105.                                 for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) && 
  106.                                         (CurrentFileName[outindex] != '\0'); outindex++) {
  107.                                     agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
  108.                                 }
  109.                                 if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer+1>=agrep_outlen)) {
  110.                                     OUTPUT_OVERFLOW;
  111.                                     /*
  112.                                     if (Text == -1) {
  113.                                         memcpy(buffer+end-D_length, tempbuf, D_length+1);
  114.                                     }
  115.                                     */
  116.                                     free_buf(Text, buffer);
  117.                                     return -1;
  118.                                 }
  119.                                 else agrep_outbuffer[agrep_outpointer+outindex++] = '\n';
  120.                                 agrep_outpointer += outindex;
  121.                             }
  122.                             /*
  123.                             if (Text == -1) {
  124.                                 memcpy(buffer+end-D_length, tempbuf, D_length+1);
  125.                             }
  126.                             */
  127.                             free_buf(Text, buffer);
  128.                             NEW_FILE = OFF;
  129.                             return 0;       
  130.                         } 
  131.                         if((Text != -1) && !(lasti >= Max_record + num_read - 1)) {
  132.                             if (-1 == output(buffer, lasti, i-D_length-1, j)) {free_buf(Text, buffer); return -1;}
  133.                         }
  134.                         else if ((Text == -1) && !(lasti >= num_read)) {
  135.                             if (-1 == output(buffer, lasti, i-D_length-1, j)) {free_buf(Text, buffer); return -1;}
  136.                         }
  137.                         if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
  138.                             ((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
  139.                             free_buf(Text, buffer);
  140.                             return 0;    /* done */
  141.                         }
  142.                     }
  143.                     lasti = i - D_length;
  144.                     TRUNCATE = OFF;
  145.                     for(k = D; k <= r4 ; k++) A[k] = B[k] = Init[0];
  146.                     r1 = Init1 & B[D];
  147.                     A[D] = (((B[D] >> 1) & CMask )  | r1) & D_Mask;
  148.                     for(k = r3; k <= r4; k++)  /* r3 = D+1, r4 = 2*D */
  149.                     { 
  150.                         r5 = B[k];
  151.                         r1 = Init1 & r5;
  152.                         A[k] = ((r5 >> 1) & CMask) | B[k-I] | (((A[k-DD] | B[k-S]) >>1) & r_NO_ERR) | r1 ; 
  153.                     }
  154.                     if (DELIMITER) CurrentByteOffset += 1*D_length;
  155.                     else CurrentByteOffset += 1*1;
  156.                 }  /* end if (A[D]&endpos) */
  157.                 CMask = Mask[buffer[i++]];
  158.                 CurrentByteOffset ++;
  159.                 r1 = A[D] & Init1;
  160.                 B[D] = ((A[D] >> 1) & CMask) | r1;
  161.                 for(k = r3; k <= r4; k++)
  162.                 { 
  163.                     r1 = A[k] & Init1;
  164.                     B[k] = ((A[k] >> 1) & CMask) | A[k-I] | (((B[k-DD] | A[k-S]) >>1)&r_NO_ERR) | r1 ; 
  165.                 }
  166.                 if(B[D] & endpos)  {  
  167.                     j++;
  168.                     if (DELIMITER) CurrentByteOffset -= D_length;
  169.                     else CurrentByteOffset -= 1;
  170.                     if(((AND == 1) && ((B[r4] & endposition) == endposition)) || ((AND == 0) && (B[r4] & endposition)) ^ INVERSE )
  171.                     { 
  172.                         if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
  173.                             num_of_matched++;
  174.                             if (agrep_finalfp != NULL) 
  175.                                 fprintf(agrep_finalfp, "%s\n", CurrentFileName);
  176.                             else {
  177.                                 int outindex;
  178.                                 for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) && 
  179.                                         (CurrentFileName[outindex] != '\0'); outindex++) {
  180.                                     agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
  181.                                 }
  182.                                 if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer+1>=agrep_outlen)) {
  183.                                     OUTPUT_OVERFLOW;
  184.                                     /*
  185.                                     if (Text == -1) {
  186.                                         memcpy(buffer+end-D_length, tempbuf, D_length+1);
  187.                                     }
  188.                                     */
  189.                                     free_buf(Text, buffer);
  190.                                     return -1;
  191.                                 }
  192.                                 else agrep_outbuffer[agrep_outpointer+outindex++] = '\n';
  193.                                 agrep_outpointer += outindex;
  194.                             }
  195.                             /*
  196.                             if (Text == -1) {
  197.                                 memcpy(buffer+end-D_length, tempbuf, D_length+1);
  198.                             }
  199.                             */
  200.                             free_buf(Text, buffer);
  201.                             NEW_FILE = OFF;
  202.                             return 0;
  203.                         }
  204.                         if((Text != -1) && !(lasti >= Max_record + num_read - 1)) {
  205.                             if (-1 == output(buffer, lasti, i-D_length-1, j)) {free_buf(Text, buffer); return -1;}
  206.                         }
  207.                         else if ((Text == -1) && !(lasti >= num_read)) {
  208.                             if (-1 == output(buffer, lasti, i-D_length-1, j)) {free_buf(Text, buffer); return -1;}
  209.                         }
  210.                         if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
  211.                             ((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
  212.                             free_buf(Text, buffer);
  213.                             return 0;    /* done */
  214.                         }
  215.                     } 
  216.                     lasti = i-D_length; 
  217.                     TRUNCATE = OFF;
  218.                     for(k=D; k <= r4; k++) A[k] = B[k] = Init[0];
  219.                     r1 = Init1 & A[D];
  220.                     B[D] = (((A[D] >> 1) & CMask )  | r1) & D_Mask;
  221.                     for(k = r3; k <= r4; k++)  /* r3 = D+1, r4 = 2*D */
  222.                     { 
  223.                         r5 = A[k];
  224.                         r1 = Init1 & r5;
  225.                         B[k] = ((r5 >> 1) & CMask) | A[k-I] | (((B[k-DD] | A[k-S]) >>1) & r_NO_ERR) | r1 ; 
  226.                     }
  227.                     if (DELIMITER) CurrentByteOffset += 1*D_length;
  228.                     else CurrentByteOffset += 1*1;
  229.                 }  /* end if (B[D]&endpos) */
  230.             }
  231.  
  232.             ResidueSize = Max_record + num_read - lasti;
  233.             if(ResidueSize > Max_record) {
  234.                 ResidueSize = Max_record;
  235.                 TRUNCATE = ON;   
  236.             }
  237.             strncpy(buffer+Max_record-ResidueSize, buffer+lasti, ResidueSize);
  238.             lasti = Max_record - ResidueSize;
  239.             if(lasti < 0) lasti = 1;
  240.             if(num_read < BlockSize) lasti = Max_record;
  241.         }
  242.         free_buf(Text, buffer);
  243.         return 0;
  244. #if    AGREP_POINTER
  245.     }
  246.     else {
  247.         lasti = 1;
  248.         /* if (DELIMITER) tempbuf = (CHAR*)malloc(D_length + 1); */
  249.         buffer = (CHAR *)agrep_inbuffer;
  250.         num_read = agrep_inlen;
  251.         end = num_read;
  252.         /* buffer[end-1] = '\n';*/ /* at end of the text. */
  253.         /* buffer[0] = '\n';*/  /* in front of the  text. */
  254.         i = 0;
  255.  
  256.         if(DELIMITER) {
  257.             for(k=0; k<D_length; k++) {
  258.                 if(old_D_pat[k] != buffer[k]) break;
  259.             }
  260.             if(k>=D_length) j--;
  261.             /*
  262.             memcpy(tempbuf, buffer+end, D_length+1);
  263.             strncpy(buffer+end, old_D_pat, D_length);
  264.             buffer[end+D_length] = '\0';
  265.             end = end + D_length;
  266.             */
  267.         }
  268.  
  269.             /* An exact copy of the above ASEARCH1_PROCESS: the while-loop below */
  270.             while (i < end)
  271.             {
  272.                 CMask = Mask[buffer[i++]];
  273.                 CurrentByteOffset ++;
  274.                 r1 = Init1 & B[D];
  275.                 A[D] = ((B[D] >> 1) & CMask )  | r1;
  276.                 for(k = r3; k <= r4; k++)  /* r3 = D+1, r4 = 2*D */
  277.                 { 
  278.                     r5 = B[k];
  279.                     r1 = Init1 & r5;
  280.                     A[k] = ((r5 >> 1) & CMask) | B[k-I] | (((A[k-DD] | B[k-S]) >>1) & r_NO_ERR) | r1 ; 
  281.                 }
  282.                 if(A[D] & endpos) {  
  283.                     j++;
  284.                     if (DELIMITER) CurrentByteOffset -= D_length;
  285.                     else CurrentByteOffset -= 1;
  286.                     if(((AND == 1) && ((A[D*2] & endposition) == endposition)) || ((AND == 0) && (A[D*2] & endposition)) ^ INVERSE )
  287.                     { 
  288.                         if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
  289.                             num_of_matched++;
  290.                             if (agrep_finalfp != NULL) 
  291.                                 fprintf(agrep_finalfp, "%s\n", CurrentFileName);
  292.                             else {
  293.                                 int outindex;
  294.                                 for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) && 
  295.                                         (CurrentFileName[outindex] != '\0'); outindex++) {
  296.                                     agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
  297.                                 }
  298.                                 if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer+1>=agrep_outlen)) {
  299.                                     OUTPUT_OVERFLOW;
  300.                                     /*
  301.                                     if (Text == -1) {
  302.                                         memcpy(buffer+end-D_length, tempbuf, D_length+1);
  303.                                     }
  304.                                     */
  305.                                     free_buf(Text, buffer);
  306.                                     return -1;
  307.                                 }
  308.                                 else agrep_outbuffer[agrep_outpointer+outindex++] = '\n';
  309.                                 agrep_outpointer += outindex;
  310.                             }
  311.                             /*
  312.                             if (Text == -1) {
  313.                                 memcpy(buffer+end-D_length, tempbuf, D_length+1);
  314.                             }
  315.                             */
  316.                             free_buf(Text, buffer);
  317.                             NEW_FILE = OFF;
  318.                             return 0;       
  319.                         } 
  320.                         if((Text != -1) && !(lasti >= Max_record + num_read - 1)) {
  321.                             if (-1 == output(buffer, lasti, i-D_length-1, j)) {free_buf(Text, buffer); return -1;}
  322.                         }
  323.                         else if ((Text == -1) && !(lasti >= num_read)) {
  324.                             if (-1 == output(buffer, lasti, i-D_length-1, j)) {free_buf(Text, buffer); return -1;}
  325.                         }
  326.                         if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
  327.                             ((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
  328.                             free_buf(Text, buffer);
  329.                             return 0;    /* done */
  330.                         }
  331.                     }
  332.                     lasti = i - D_length;
  333.                     TRUNCATE = OFF;
  334.                     for(k = D; k <= r4 ; k++) A[k] = B[k] = Init[0];
  335.                     r1 = Init1 & B[D];
  336.                     A[D] = (((B[D] >> 1) & CMask )  | r1) & D_Mask;
  337.                     for(k = r3; k <= r4; k++)  /* r3 = D+1, r4 = 2*D */
  338.                     { 
  339.                         r5 = B[k];
  340.                         r1 = Init1 & r5;
  341.                         A[k] = ((r5 >> 1) & CMask) | B[k-I] | (((A[k-DD] | B[k-S]) >>1) & r_NO_ERR) | r1 ; 
  342.                     }
  343.                     if (DELIMITER) CurrentByteOffset += 1*D_length;
  344.                     else CurrentByteOffset += 1*1;
  345.                 }  /* end if (A[D]&endpos) */
  346.                 CMask = Mask[buffer[i++]];
  347.                 CurrentByteOffset ++;
  348.                 r1 = A[D] & Init1;
  349.                 B[D] = ((A[D] >> 1) & CMask) | r1;
  350.                 for(k = r3; k <= r4; k++)
  351.                 { 
  352.                     r1 = A[k] & Init1;
  353.                     B[k] = ((A[k] >> 1) & CMask) | A[k-I] | (((B[k-DD] | A[k-S]) >>1)&r_NO_ERR) | r1 ; 
  354.                 }
  355.                 if(B[D] & endpos)  {  
  356.                     j++;
  357.                     if (DELIMITER) CurrentByteOffset -= D_length;
  358.                     else CurrentByteOffset -= 1;
  359.                     if(((AND == 1) && ((B[r4] & endposition) == endposition)) || ((AND == 0) && (B[r4] & endposition)) ^ INVERSE )
  360.                     { 
  361.                         if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
  362.                             num_of_matched++;
  363.                             if (agrep_finalfp != NULL) 
  364.                                 fprintf(agrep_finalfp, "%s\n", CurrentFileName);
  365.                             else {
  366.                                 int outindex;
  367.                                 for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) && 
  368.                                         (CurrentFileName[outindex] != '\0'); outindex++) {
  369.                                     agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
  370.                                 }
  371.                                 if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer+1>=agrep_outlen)) {
  372.                                     OUTPUT_OVERFLOW;
  373.                                     /*
  374.                                     if (Text == -1) {
  375.                                         memcpy(buffer+end-D_length, tempbuf, D_length+1);
  376.                                     }
  377.                                     */
  378.                                     free_buf(Text, buffer);
  379.                                     return -1;
  380.                                 }
  381.                                 else agrep_outbuffer[agrep_outpointer+outindex++] = '\n';
  382.                                 agrep_outpointer += outindex;
  383.                             }
  384.                             /*
  385.                             if (Text == -1) {
  386.                                 memcpy(buffer+end-D_length, tempbuf, D_length+1);
  387.                             }
  388.                             */
  389.                             free_buf(Text, buffer);
  390.                             NEW_FILE = OFF;
  391.                             return 0;
  392.                         }
  393.                         if((Text != -1) && !(lasti >= Max_record + num_read - 1)) {
  394.                             if (-1 == output(buffer, lasti, i-D_length-1, j)) {free_buf(Text, buffer); return -1;}
  395.                         }
  396.                         else if ((Text == -1) && !(lasti >= num_read)) {
  397.                             if (-1 == output(buffer, lasti, i-D_length-1, j)) {free_buf(Text, buffer); return -1;}
  398.                         }
  399.                         if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
  400.                             ((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
  401.                             free_buf(Text, buffer);
  402.                             return 0;    /* done */
  403.                         }
  404.                     } 
  405.                     lasti = i-D_length; 
  406.                     TRUNCATE = OFF;
  407.                     for(k=D; k <= r4; k++) A[k] = B[k] = Init[0];
  408.                     r1 = Init1 & A[D];
  409.                     B[D] = (((A[D] >> 1) & CMask )  | r1) & D_Mask;
  410.                     for(k = r3; k <= r4; k++)  /* r3 = D+1, r4 = 2*D */
  411.                     { 
  412.                         r5 = A[k];
  413.                         r1 = Init1 & r5;
  414.                         B[k] = ((r5 >> 1) & CMask) | A[k-I] | (((B[k-DD] | A[k-S]) >>1) & r_NO_ERR) | r1 ; 
  415.                     }
  416.                     if (DELIMITER) CurrentByteOffset += 1*D_length;
  417.                     else CurrentByteOffset += 1*1;
  418.                 }  /* end if (B[D]&endpos) */
  419.             }
  420.  
  421.         /*
  422.         if (DELIMITER) {
  423.             memcpy(buffer+end, tempbuf, D_length+1);
  424.             free(tempbuf);
  425.         }
  426.         */
  427.         return 0;
  428.     }
  429. #endif    /*AGREP_POINTER*/
  430. }
  431.  
  432.